﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using UltiCoreLibrary;

namespace UltiSheet
{
    public partial class PartySetup : Form
    {
        private Boolean canUpdateUI = true;

        private Player nextDealer;

        private GameState gameState;

        public GameState GameState
        {
            set { gameState = value; }
        }

        public PartySetup()
        {
            InitializeComponent();
        }

        private Game CalculateGameValue()
        {
            GameColor gameColor = null; // new GameColor(GameColors.COLOR_COLORLESS);

            LinkedList<BasicGame> subGames = new LinkedList<BasicGame>();

            if (checkBoxParty.Checked)
            {
                subGames.AddFirst(new BasicGame(BasicGameType.GAME_PARTY, false, false, false));
            }
            if (checkBox4Ace.Checked)
            {
                subGames.AddFirst(new BasicGame(BasicGameType.GAME_4ACE, false, false, false));
            }
            if (checkBoxUlti.Checked)
            {
                subGames.AddFirst(new BasicGame(BasicGameType.GAME_ULTI, false, false, false));
            }
            if (checkBox40.Checked)
            {
                subGames.AddFirst(new BasicGame(BasicGameType.GAME_40, false, false, false));
            }
            if (checkBox20.Checked)
            {
                subGames.AddFirst(new BasicGame(BasicGameType.GAME_20, false, false, false));
            }
            if (checkBoxDurchmars.Checked)
            {
                subGames.AddFirst(new BasicGame(BasicGameType.GAME_DURCHMARS, false, false, false));
            }
            if (checkBoxReDurchmars.Checked)
            {
                subGames.AddFirst(new BasicGame(BasicGameType.GAME_DURCHMARS, false, true, false));
            }
            if (checkBoxHearts.Checked)
            {
                foreach (BasicGame basicGame in subGames)
                {
                    gameColor = new GameColor(GameColors.COLOR_HEARTS);
                    basicGame.Hearts = true;
                }
            }
            foreach (BasicGame basicGame in BasicGame.colorlessGames)
            {
                if (basicGame == comboBoxColorless.SelectedItem)
                {
                    subGames.AddFirst(basicGame);
                    gameColor = new GameColor(GameColors.COLOR_COLORLESS);
                    break;
                }
            }
            if (gameColor == null)
            {
                foreach (GameColor color in GameColor.colors)
                {
                    if (color == comboBoxColor.SelectedItem)
                    {
                        gameColor = color;
                    }
                }
            }
            Game game = new Game(subGames, gameColor);
            return game;
        }

        private void PartySetup_Closing(object sender, CancelEventArgs e)
        {
            if (
                !checkBoxPlayer1.Checked &&
                !checkBoxPlayer2.Checked &&
                !checkBoxPlayer3.Checked)
            {
                MessageBox.Show(
                    "The party's point is not selected.",
                    "Party setup error!",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation,
                    MessageBoxDefaultButton.Button1);
                e.Cancel = true;
                return;
            }
            Game g = CalculateGameValue();
            if (g.Trump == null)
            {
                MessageBox.Show(
                    "You must select a colorless game or select a trump.",
                    "Party setup error!",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation,
                    MessageBoxDefaultButton.Button1);
                e.Cancel = true;
                return;
            }
            if (MessageBox.Show(
                g.Name + " " + g.Value,
                "Game setup",
                MessageBoxButtons.OKCancel,
                MessageBoxIcon.Question,
                MessageBoxDefaultButton.Button1) == DialogResult.Cancel)
            {
                e.Cancel = true;
                return;
            }

            Player point = null;
            Player defender1 = null;
            Player defender2 = null;
            if (checkBoxPlayer1.Checked)
            {
                point = gameState.players[0];
                defender1 = gameState.players[1];
                defender2 = gameState.players[2];
            }
            if (checkBoxPlayer2.Checked)
            {
                point = gameState.players[1];
                defender1 = gameState.players[2];
                defender2 = gameState.players[0];
            }
            if (checkBoxPlayer3.Checked)
            {
                point = gameState.players[2];
                defender1 = gameState.players[0];
                defender2 = gameState.players[1];
            }
            Party p = new Party(
                nextDealer,
                g,
                point,
                defender1,
                defender2);
            gameState.parties.AddLast(p);

        }

        private void PartySetup_Activated(object sender, EventArgs e)
        {
            nextDealer = gameState.players[0];
            if (gameState.parties.Last != null)
            {
                Player lastDealer = gameState.parties.Last.Value.Dealer;
                for (int i = 0; i < 3; i++)
                {
                    if (gameState.players[i] == lastDealer)
                    {
                        if (i != 2)
                        {
                            nextDealer = gameState.players[i + 1];
                        }
                        else
                        {
                            nextDealer = gameState.players[0];
                        }
                    }
                }
            }
            checkBoxPlayer1.Text = gameState.players[0].Name;
            if (nextDealer == gameState.players[0])
            {
                checkBoxPlayer1.ForeColor = Color.Blue;
            }
            checkBoxPlayer2.Text = gameState.players[1].Name;
            if (nextDealer == gameState.players[1])
            {
                checkBoxPlayer2.ForeColor = Color.Blue;
            }
            checkBoxPlayer3.Text = gameState.players[2].Name;
            if (nextDealer == gameState.players[2])
            {
                checkBoxPlayer3.ForeColor = Color.Blue;
            }
            foreach (BasicGame game in BasicGame.colorlessGames)
            {
                comboBoxColorless.Items.Add(game);
            }
            foreach (GameColor color in GameColor.colors)
            {
                comboBoxColor.Items.Add(color);
            }
        }

        private void UnCheckColoredGames()
        {
            if (!canUpdateUI)
            {
                return;
            }
            canUpdateUI = false;
            checkBox20.Checked = false;
            checkBox40.Checked = false;
            checkBoxDurchmars.Checked = false;
            checkBoxHearts.Checked = false;
            checkBoxParty.Checked = false;
            checkBoxReDurchmars.Checked = false;
            checkBoxUlti.Checked = false;
            checkBox4Ace.Checked = false;
            canUpdateUI = true;
        }

        # region Auction items
        private void comboBoxColorless_SelectedValueChanged(object sender, EventArgs e)
        {
            UnCheckColoredGames();
        }

        private void checkBoxParty_CheckStateChanged(object sender, EventArgs e)
        {
            if (!canUpdateUI)
            {
                return;
            }
            canUpdateUI = false;
            if (checkBoxParty.CheckState == CheckState.Checked)
            {
                checkBox20.Checked = false;
                checkBox40.Checked = false;
                checkBoxDurchmars.Checked = false;
                checkBoxReDurchmars.Checked = false;
                comboBoxColorless.SelectedItem = null;
            }
            else if (checkBoxParty.CheckState == CheckState.Unchecked)
            {
                checkBoxUlti.Checked = false;
                checkBox40.Checked = false;
            }
            canUpdateUI = true;
        }

        private void checkBox40_CheckStateChanged(object sender, EventArgs e)
        {
            if (!canUpdateUI)
            {
                return;
            }
            canUpdateUI = false;
            if (checkBox40.CheckState == CheckState.Checked)
            {
                checkBoxParty.Checked = false;
                checkBox20.Checked = false;
                comboBoxColorless.SelectedItem = null;
            }
            canUpdateUI = true;
        }

        private void checkBox4Ace_CheckStateChanged(object sender, EventArgs e)
        {
            if (!canUpdateUI)
            {
                return;
            }
            canUpdateUI = false;
            if (checkBox4Ace.CheckState == CheckState.Checked)
            {
                if (
                    checkBox20.CheckState != CheckState.Checked &&
                    checkBox40.CheckState != CheckState.Checked &&
                    checkBoxDurchmars.CheckState != CheckState.Checked &&
                    checkBoxReDurchmars.CheckState != CheckState.Checked)
                {
                    checkBoxParty.Checked = true;
                }
                checkBoxDurchmars.Checked = false;
                checkBoxReDurchmars.Checked = false;
                comboBoxColorless.SelectedItem = null;
            }
            canUpdateUI = true;

        }

        private void checkBoxUlti_CheckStateChanged(object sender, EventArgs e)
        {
            if (!canUpdateUI)
            {
                return;
            }
            canUpdateUI = false;
            if (checkBoxUlti.CheckState == CheckState.Checked)
            {
                if (
                    checkBox20.CheckState != CheckState.Checked &&
                    checkBox40.CheckState != CheckState.Checked &&
                    checkBoxDurchmars.CheckState != CheckState.Checked &&
                    checkBoxReDurchmars.CheckState != CheckState.Checked)
                {
                    checkBoxParty.Checked = true;
                }
                comboBoxColorless.SelectedItem = null;
            }
            canUpdateUI = true;

        }

        private void checkBox20_CheckStateChanged(object sender, EventArgs e)
        {
            if (!canUpdateUI)
            {
                return;
            }
            canUpdateUI = false;
            if (checkBox20.CheckState == CheckState.Checked)
            {
                checkBoxParty.Checked = false;
                checkBox40.Checked = false;
                comboBoxColorless.SelectedItem = null;
            }
            canUpdateUI = true;

        }

        private void checkBoxDurchmars_CheckStateChanged(object sender, EventArgs e)
        {
            if (!canUpdateUI)
            {
                return;
            }
            canUpdateUI = false;
            if (checkBoxDurchmars.CheckState == CheckState.Checked)
            {
                checkBoxParty.Checked = false;
                checkBox4Ace.Checked = false;
                checkBoxReDurchmars.Checked = false;
                comboBoxColorless.SelectedItem = null;
            }
            canUpdateUI = true;

        }

        private void checkBoxReDurchmars_CheckStateChanged(object sender, EventArgs e)
        {
        
            if (!canUpdateUI)
            {
                return;
            }
            canUpdateUI = false;
            if (checkBoxReDurchmars.CheckState == CheckState.Checked)
            {
                checkBoxParty.Checked = false;
                checkBox4Ace.Checked = false;
                checkBoxDurchmars.Checked = false;
                comboBoxColorless.SelectedItem = null;
            }
            canUpdateUI = true;
        }

        private void checkBoxHearts_CheckStateChanged(object sender, EventArgs e)
        {
            if (!canUpdateUI)
            {
                return;
            }
            canUpdateUI = false;
            if (checkBoxHearts.CheckState == CheckState.Checked)
            {
                comboBoxColorless.SelectedItem = null;
                comboBoxColor.SelectedItem = null;
            }
            canUpdateUI = true;
        }

        private void comboBoxColor_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!canUpdateUI)
            {
                return;
            }
            canUpdateUI = false;
            checkBoxHearts.Checked = false;
            canUpdateUI = true;
        }

        # endregion

        # region Point selection
        private void checkBoxPlayer1_CheckStateChanged(object sender, EventArgs e)
        {
            if (checkBoxPlayer1.Checked)
            {
                checkBoxPlayer2.CheckState = CheckState.Unchecked;
                checkBoxPlayer3.CheckState = CheckState.Unchecked;
            }
        }

        private void checkBoxPlayer2_CheckStateChanged(object sender, EventArgs e)
        {
            if (checkBoxPlayer1.Checked)
            {
                checkBoxPlayer1.CheckState = CheckState.Unchecked;
                checkBoxPlayer3.CheckState = CheckState.Unchecked;
            }
        }
        private void checkBoxPlayer3_CheckStateChanged(object sender, EventArgs e)
        {
            if (checkBoxPlayer3.Checked)
            {
                checkBoxPlayer2.CheckState = CheckState.Unchecked;
                checkBoxPlayer1.CheckState = CheckState.Unchecked;
            }
        }
        #endregion
    }
}